<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 技术教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="上次写了一篇 《Elasticsearch快速入门，掌握这些刚刚好！》，带大家学习了下 Elasticsearch 的基本用法，这次我们来篇实战教程，以 mall 项目中的商品搜索为例，把 Elasticsearch 用起来！  #👍 相关视频教程 商品搜索功能设计与实现_上篇open in new window 商品搜索功能设计与实现_下篇open in new window  #中文分词器">
<meta property="og:type" content="article">
<meta property="og:title" content="Elasticsearch项目实战，商品搜索功能设计与实现！">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2024/01/d8d5c5f413d7.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="上次写了一篇 《Elasticsearch快速入门，掌握这些刚刚好！》，带大家学习了下 Elasticsearch 的基本用法，这次我们来篇实战教程，以 mall 项目中的商品搜索为例，把 Elasticsearch 用起来！  #👍 相关视频教程 商品搜索功能设计与实现_上篇open in new window 商品搜索功能设计与实现_下篇open in new window  #中文分词器">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2024-01-21T09:27:56.000Z">
<meta property="article:modified_time" content="2024-02-02T14:52:25.301Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="宅男,热血">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2024/01/d8d5c5f413d7.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 技术教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-02 22:52:25'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">Elasticsearch项目实战，商品搜索功能设计与实现！</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-01-21T09:27:56.000Z" title="发表于 2024-01-21 17:27:56">2024-01-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-02T14:52:25.301Z" title="更新于 2024-02-02 22:52:25">2024-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/">技术教程</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/">mall</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%8A%80%E6%9C%AF%E6%95%99%E7%A8%8B/mall/%E6%8A%80%E6%9C%AF%E8%A6%81%E7%82%B9%E7%AF%87/">技术要点篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>14分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Elasticsearch项目实战，商品搜索功能设计与实现！"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><blockquote>
<p>上次写了一篇 <a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/elasticsearch_start.html">《Elasticsearch快速入门，掌握这些刚刚好！》</a>，带大家学习了下 Elasticsearch 的基本用法，这次我们来篇实战教程，以 <code>mall</code> 项目中的商品搜索为例，把 Elasticsearch 用起来！</p>
</blockquote>
<h2 id="👍-相关视频教程"><a href="#👍-相关视频教程" class="headerlink" title="#👍 相关视频教程"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%F0%9F%91%8D-%E7%9B%B8%E5%85%B3%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B">#</a>👍 相关视频教程</h2><ul>
<li><a target="_blank" rel="noopener" href="https://t.zsxq.com/0fpL2qYBj">商品搜索功能设计与实现_上篇open in new window</a></li>
<li><a target="_blank" rel="noopener" href="https://t.zsxq.com/0fxacah0j">商品搜索功能设计与实现_下篇open in new window</a></li>
</ul>
<h2 id="中文分词器"><a href="#中文分词器" class="headerlink" title="#中文分词器"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8">#</a>中文分词器</h2><blockquote>
<p>由于商品搜索会涉及中文搜索，Elasticsearch需要安装插件才可以支持，我们先来了解下中文分词器，这里使用的是IKAnalyzer。在<a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/reference/elasticsearch_start.html">《Elasticsearch快速入门，掌握这些刚刚好！》</a>中已经讲过其安装方式，这里直接讲解它的用法。</p>
</blockquote>
<h3 id="使用IKAnalyzer"><a href="#使用IKAnalyzer" class="headerlink" title="#使用IKAnalyzer"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E4%BD%BF%E7%94%A8ikanalyzer">#</a>使用IKAnalyzer</h3><ul>
<li>使用默认分词器，可以发现默认分词器只是将中文逐词分隔，并不符合我们的需求；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GET /pms/_analyze</span><br><span class="line">&#123;</span><br><span class="line">  &quot;text&quot;: &quot;小米手机性价比很高&quot;,</span><br><span class="line">  &quot;tokenizer&quot;: &quot;standard&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_01-8170d4aa.png"></p>
<ul>
<li>使用中文分词器以后，可以将中文文本按语境进行分隔，可以满足我们的需求。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GET /pms/_analyze</span><br><span class="line">&#123;</span><br><span class="line">  &quot;text&quot;: &quot;小米手机性价比很高&quot;,</span><br><span class="line">  &quot;tokenizer&quot;: &quot;ik_max_word&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_02-14f2b956.png"></p>
<h3 id="在SpringBoot中使用"><a href="#在SpringBoot中使用" class="headerlink" title="#在SpringBoot中使用"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E5%9C%A8springboot%E4%B8%AD%E4%BD%BF%E7%94%A8">#</a>在SpringBoot中使用</h3><p>在SpringBoot中使用Elasticsearch本文不再赘述，直接参考<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/op7CTQS5dGOKv5BvRbeFow">《mall整合Elasticsearch实现商品搜索》open in new window</a>即可。这里需要提一下，对于需要进行中文分词的字段，我们直接使用@Field注解将analyzer属性设置为<code>ik_max_word</code>即可。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 搜索中的商品信息</span><br><span class="line"> * Created by macro on 2018/6/19.</span><br><span class="line"> */</span><br><span class="line">@Document(indexName = &quot;pms&quot;, type = &quot;product&quot;,shards = 1,replicas = 0)</span><br><span class="line">public class EsProduct implements Serializable &#123;</span><br><span class="line">    private static final long serialVersionUID = -1L;</span><br><span class="line">    @Id</span><br><span class="line">    private Long id;</span><br><span class="line">    @Field(analyzer = &quot;ik_max_word&quot;,type = FieldType.Text)</span><br><span class="line">    private String name;</span><br><span class="line">    @Field(analyzer = &quot;ik_max_word&quot;,type = FieldType.Text)</span><br><span class="line">    private String subTitle;</span><br><span class="line">    @Field(analyzer = &quot;ik_max_word&quot;,type = FieldType.Text)</span><br><span class="line">    private String keywords;</span><br><span class="line">    //省略若干代码......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="简单商品搜索"><a href="#简单商品搜索" class="headerlink" title="#简单商品搜索"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E7%AE%80%E5%8D%95%E5%95%86%E5%93%81%E6%90%9C%E7%B4%A2">#</a>简单商品搜索</h2><blockquote>
<p>我们先来实现一个最简单的商品搜索，搜索商品名称、副标题、关键词中包含指定关键字的商品。</p>
</blockquote>
<ul>
<li>使用Query DSL调用Elasticsearch的Restful API实现；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">POST /pms/product/_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;from&quot;: 0, </span><br><span class="line">  &quot;size&quot;: 2, </span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;multi_match&quot;: &#123;</span><br><span class="line">      &quot;query&quot;: &quot;小米&quot;,</span><br><span class="line">      &quot;fields&quot;: [</span><br><span class="line">        &quot;name&quot;,</span><br><span class="line">        &quot;subTitle&quot;,</span><br><span class="line">        &quot;keywords&quot;</span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_03-c91faee0.png"></p>
<ul>
<li>在SpringBoot中实现，使用Elasticsearch Repositories的衍生查询来搜索；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 商品搜索管理Service实现类</span><br><span class="line"> * Created by macro on 2018/6/19.</span><br><span class="line"> */</span><br><span class="line">@Service</span><br><span class="line">public class EsProductServiceImpl implements EsProductService &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public Page&lt;EsProduct&gt; search(String keyword, Integer pageNum, Integer pageSize) &#123;</span><br><span class="line">        Pageable pageable = PageRequest.of(pageNum, pageSize);</span><br><span class="line">        return productRepository.findByNameOrSubTitleOrKeywords(keyword, keyword, keyword, pageable);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>衍生查询其实原理很简单，就是将一定规则方法名称的方法转化为Elasticsearch的Query DSL语句，看完下面这张表你就懂了。</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_04-3ad5b636.png"></p>
<h2 id="综合商品搜索"><a href="#综合商品搜索" class="headerlink" title="#综合商品搜索"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E7%BB%BC%E5%90%88%E5%95%86%E5%93%81%E6%90%9C%E7%B4%A2">#</a>综合商品搜索</h2><blockquote>
<p>接下来我们来实现一个复杂的商品搜索，涉及到过滤、不同字段匹配权重不同以及可以进行排序。</p>
</blockquote>
<ul>
<li>首先来说下我们的需求，按输入的关键字搜索商品名称、副标题和关键词，可以按品牌和分类进行筛选，可以有5种排序方式，默认按相关度进行排序，看下接口文档有助于理解；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_05-4f5d3259.png"></p>
<ul>
<li><p>这里我们有一点特殊的需求，比如商品名称匹配关键字的的商品我们认为与搜索条件更匹配，其次是副标题和关键字，这时就需要用到<code>function_score</code>查询了；</p>
</li>
<li><p>在Elasticsearch中搜索到文档的相关性由<code>_score</code>字段来表示的，文档的<code>_score</code>字段值越高，表示与搜索条件越匹配，而<code>function_score</code>查询可以通过设置权重来影响<code>_score</code>字段值，使用它我们就可以实现上面的需求了；</p>
</li>
<li><p>使用Query DSL调用Elasticsearch的Restful API实现，可以发现商品名称权重设置为了10，商品副标题权重设置为了5，商品关键字设置为了2；</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">POST /pms/product/_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;function_score&quot;: &#123;</span><br><span class="line">      &quot;query&quot;: &#123;</span><br><span class="line">        &quot;bool&quot;: &#123;</span><br><span class="line">          &quot;must&quot;: [</span><br><span class="line">            &#123;</span><br><span class="line">              &quot;match_all&quot;: &#123;&#125;</span><br><span class="line">            &#125;</span><br><span class="line">          ],</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;bool&quot;: &#123;</span><br><span class="line">              &quot;must&quot;: [</span><br><span class="line">                &#123;</span><br><span class="line">                  &quot;term&quot;: &#123;</span><br><span class="line">                    &quot;brandId&quot;: 6</span><br><span class="line">                  &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                  &quot;term&quot;: &#123;</span><br><span class="line">                    &quot;productCategoryId&quot;: 19</span><br><span class="line">                  &#125;</span><br><span class="line">                &#125;</span><br><span class="line">              ]</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &quot;functions&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;match&quot;: &#123;</span><br><span class="line">              &quot;name&quot;: &quot;小米&quot;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 10</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;match&quot;: &#123;</span><br><span class="line">              &quot;subTitle&quot;: &quot;小米&quot;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 5</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;match&quot;: &#123;</span><br><span class="line">              &quot;keywords&quot;: &quot;小米&quot;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 2</span><br><span class="line">        &#125;</span><br><span class="line">      ],</span><br><span class="line">      &quot;score_mode&quot;: &quot;sum&quot;,</span><br><span class="line">      &quot;min_score&quot;: 2</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;sort&quot;: [</span><br><span class="line">    &#123;</span><br><span class="line">      &quot;_score&quot;: &#123;</span><br><span class="line">        &quot;order&quot;: &quot;desc&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_06-ecca0e08.png"></p>
<ul>
<li>在SpringBoot中实现，使用Elasticsearch Repositories的search方法来实现，但需要自定义查询条件QueryBuilder；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 商品搜索管理Service实现类</span><br><span class="line"> * Created by macro on 2018/6/19.</span><br><span class="line"> */</span><br><span class="line">@Service</span><br><span class="line">public class EsProductServiceImpl implements EsProductService &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public Page&lt;EsProduct&gt; search(String keyword, Long brandId, Long productCategoryId, Integer pageNum, Integer pageSize,Integer sort) &#123;</span><br><span class="line">        Pageable pageable = PageRequest.of(pageNum, pageSize);</span><br><span class="line">        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();</span><br><span class="line">        //分页</span><br><span class="line">        nativeSearchQueryBuilder.withPageable(pageable);</span><br><span class="line">        //过滤</span><br><span class="line">        if (brandId != null || productCategoryId != null) &#123;</span><br><span class="line">            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();</span><br><span class="line">            if (brandId != null) &#123;</span><br><span class="line">                boolQueryBuilder.must(QueryBuilders.termQuery(&quot;brandId&quot;, brandId));</span><br><span class="line">            &#125;</span><br><span class="line">            if (productCategoryId != null) &#123;</span><br><span class="line">                boolQueryBuilder.must(QueryBuilders.termQuery(&quot;productCategoryId&quot;, productCategoryId));</span><br><span class="line">            &#125;</span><br><span class="line">            nativeSearchQueryBuilder.withFilter(boolQueryBuilder);</span><br><span class="line">        &#125;</span><br><span class="line">        //搜索</span><br><span class="line">        if (StringUtils.isEmpty(keyword)) &#123;</span><br><span class="line">            nativeSearchQueryBuilder.withQuery(QueryBuilders.matchAllQuery());</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            List&lt;FunctionScoreQueryBuilder.FilterFunctionBuilder&gt; filterFunctionBuilders = new ArrayList&lt;&gt;();</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;name&quot;, keyword),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(10)));</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;subTitle&quot;, keyword),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(5)));</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;keywords&quot;, keyword),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(2)));</span><br><span class="line">            FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];</span><br><span class="line">            filterFunctionBuilders.toArray(builders);</span><br><span class="line">            FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)</span><br><span class="line">                    .scoreMode(FunctionScoreQuery.ScoreMode.SUM)</span><br><span class="line">                    .setMinScore(2);</span><br><span class="line">            nativeSearchQueryBuilder.withQuery(functionScoreQueryBuilder);</span><br><span class="line">        &#125;</span><br><span class="line">        //排序</span><br><span class="line">        if(sort==1)&#123;</span><br><span class="line">            //按新品从新到旧</span><br><span class="line">            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(&quot;id&quot;).order(SortOrder.DESC));</span><br><span class="line">        &#125;else if(sort==2)&#123;</span><br><span class="line">            //按销量从高到低</span><br><span class="line">            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(&quot;sale&quot;).order(SortOrder.DESC));</span><br><span class="line">        &#125;else if(sort==3)&#123;</span><br><span class="line">            //按价格从低到高</span><br><span class="line">            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(&quot;price&quot;).order(SortOrder.ASC));</span><br><span class="line">        &#125;else if(sort==4)&#123;</span><br><span class="line">            //按价格从高到低</span><br><span class="line">            nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort(&quot;price&quot;).order(SortOrder.DESC));</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            //按相关度</span><br><span class="line">            nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));</span><br><span class="line">        &#125;</span><br><span class="line">        nativeSearchQueryBuilder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC));</span><br><span class="line">        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();</span><br><span class="line">        LOGGER.info(&quot;DSL:&#123;&#125;&quot;, searchQuery.getQuery().toString());</span><br><span class="line">        return productRepository.search(searchQuery);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="相关商品推荐"><a href="#相关商品推荐" class="headerlink" title="#相关商品推荐"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E7%9B%B8%E5%85%B3%E5%95%86%E5%93%81%E6%8E%A8%E8%8D%90">#</a>相关商品推荐</h2><blockquote>
<p>当我们查看相关商品的时候，一般底部会有一些商品推荐，这里使用Elasticsearch来简单实现下。</p>
</blockquote>
<ul>
<li>首先来说下我们的需求，可以根据指定商品的ID来查找相关商品，看下接口文档有助于理解；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_07-a92ce6b8.png"></p>
<ul>
<li><p>这里我们的实现原理是这样的：首先根据ID获取指定商品信息，然后以指定商品的名称、品牌和分类来搜索商品，并且要过滤掉当前商品，调整搜索条件中的权重以获取最好的匹配度；</p>
</li>
<li><p>使用Query DSL调用Elasticsearch的Restful API实现；</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line">POST /pms/product/_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;function_score&quot;: &#123;</span><br><span class="line">      &quot;query&quot;: &#123;</span><br><span class="line">        &quot;bool&quot;: &#123;</span><br><span class="line">          &quot;must&quot;: [</span><br><span class="line">            &#123;</span><br><span class="line">              &quot;match_all&quot;: &#123;&#125;</span><br><span class="line">            &#125;</span><br><span class="line">          ],</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;bool&quot;: &#123;</span><br><span class="line">              &quot;must_not&quot;: &#123;</span><br><span class="line">                &quot;term&quot;: &#123;</span><br><span class="line">                  &quot;id&quot;: 28</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &quot;functions&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;match&quot;: &#123;</span><br><span class="line">              &quot;name&quot;: &quot;红米5A&quot;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 8</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;match&quot;: &#123;</span><br><span class="line">              &quot;subTitle&quot;: &quot;红米5A&quot;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 2</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;match&quot;: &#123;</span><br><span class="line">              &quot;keywords&quot;: &quot;红米5A&quot;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 2</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;term&quot;: &#123;</span><br><span class="line">              &quot;brandId&quot;: 6</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 5</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;term&quot;: &#123;</span><br><span class="line">              &quot;productCategoryId&quot;: 19</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;weight&quot;: 3</span><br><span class="line">        &#125;</span><br><span class="line">      ],</span><br><span class="line">      &quot;score_mode&quot;: &quot;sum&quot;,</span><br><span class="line">      &quot;min_score&quot;: 2</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_08-21aef257.png"></p>
<ul>
<li>在SpringBoot中实现，使用Elasticsearch Repositories的search方法来实现，但需要自定义查询条件QueryBuilder；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 商品搜索管理Service实现类</span><br><span class="line"> * Created by macro on 2018/6/19.</span><br><span class="line"> */</span><br><span class="line">@Service</span><br><span class="line">public class EsProductServiceImpl implements EsProductService &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public Page&lt;EsProduct&gt; recommend(Long id, Integer pageNum, Integer pageSize) &#123;</span><br><span class="line">        Pageable pageable = PageRequest.of(pageNum, pageSize);</span><br><span class="line">        List&lt;EsProduct&gt; esProductList = productDao.getAllEsProductList(id);</span><br><span class="line">        if (esProductList.size() &gt; 0) &#123;</span><br><span class="line">            EsProduct esProduct = esProductList.get(0);</span><br><span class="line">            String keyword = esProduct.getName();</span><br><span class="line">            Long brandId = esProduct.getBrandId();</span><br><span class="line">            Long productCategoryId = esProduct.getProductCategoryId();</span><br><span class="line">            //根据商品标题、品牌、分类进行搜索</span><br><span class="line">            List&lt;FunctionScoreQueryBuilder.FilterFunctionBuilder&gt; filterFunctionBuilders = new ArrayList&lt;&gt;();</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;name&quot;, keyword),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(8)));</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;subTitle&quot;, keyword),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(2)));</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;keywords&quot;, keyword),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(2)));</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;brandId&quot;, brandId),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(5)));</span><br><span class="line">            filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery(&quot;productCategoryId&quot;, productCategoryId),</span><br><span class="line">                    ScoreFunctionBuilders.weightFactorFunction(3)));</span><br><span class="line">            FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];</span><br><span class="line">            filterFunctionBuilders.toArray(builders);</span><br><span class="line">            FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)</span><br><span class="line">                    .scoreMode(FunctionScoreQuery.ScoreMode.SUM)</span><br><span class="line">                    .setMinScore(2);</span><br><span class="line">            //用于过滤掉相同的商品</span><br><span class="line">            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();</span><br><span class="line">            boolQueryBuilder.mustNot(QueryBuilders.termQuery(&quot;id&quot;,id));</span><br><span class="line">            //构建查询条件</span><br><span class="line">            NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();</span><br><span class="line">            builder.withQuery(functionScoreQueryBuilder);</span><br><span class="line">            builder.withFilter(boolQueryBuilder);</span><br><span class="line">            builder.withPageable(pageable);</span><br><span class="line">            NativeSearchQuery searchQuery = builder.build();</span><br><span class="line">            LOGGER.info(&quot;DSL:&#123;&#125;&quot;, searchQuery.getQuery().toString());</span><br><span class="line">            return productRepository.search(searchQuery);</span><br><span class="line">        &#125;</span><br><span class="line">        return new PageImpl&lt;&gt;(null);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="聚合搜索商品相关信息"><a href="#聚合搜索商品相关信息" class="headerlink" title="#聚合搜索商品相关信息"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E8%81%9A%E5%90%88%E6%90%9C%E7%B4%A2%E5%95%86%E5%93%81%E7%9B%B8%E5%85%B3%E4%BF%A1%E6%81%AF">#</a>聚合搜索商品相关信息</h2><blockquote>
<p>在搜索商品时，经常会有一个筛选界面来帮助我们找到想要的商品，这里使用Elasticsearch来简单实现下。</p>
</blockquote>
<ul>
<li>首先来说下我们的需求，可以根据搜索关键字获取到与关键字匹配商品相关的分类、品牌以及属性，下面这张图有助于理解；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_09-8ff7f482.png"></p>
<ul>
<li><p>这里我们可以使用Elasticsearch的聚合来实现，搜索出相关商品，聚合出商品的品牌、商品的分类以及商品的属性，只要出现次数最多的前十个即可；</p>
</li>
<li><p>使用Query DSL调用Elasticsearch的Restful API实现；</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">POST /pms/product/_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;multi_match&quot;: &#123;</span><br><span class="line">      &quot;query&quot;: &quot;小米&quot;,</span><br><span class="line">      &quot;fields&quot;: [</span><br><span class="line">        &quot;name&quot;,</span><br><span class="line">        &quot;subTitle&quot;,</span><br><span class="line">        &quot;keywords&quot;</span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;size&quot;: 0,</span><br><span class="line">  &quot;aggs&quot;: &#123;</span><br><span class="line">    &quot;brandNames&quot;: &#123;</span><br><span class="line">      &quot;terms&quot;: &#123;</span><br><span class="line">        &quot;field&quot;: &quot;brandName&quot;,</span><br><span class="line">        &quot;size&quot;: 10</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;productCategoryNames&quot;: &#123;</span><br><span class="line">      &quot;terms&quot;: &#123;</span><br><span class="line">        &quot;field&quot;: &quot;productCategoryName&quot;,</span><br><span class="line">        &quot;size&quot;: 10</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;allAttrValues&quot;: &#123;</span><br><span class="line">      &quot;nested&quot;: &#123;</span><br><span class="line">        &quot;path&quot;: &quot;attrValueList&quot;</span><br><span class="line">      &#125;,</span><br><span class="line">      &quot;aggs&quot;: &#123;</span><br><span class="line">        &quot;productAttrs&quot;: &#123;</span><br><span class="line">          &quot;filter&quot;: &#123;</span><br><span class="line">            &quot;term&quot;: &#123;</span><br><span class="line">              &quot;attrValueList.type&quot;: 1</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          &quot;aggs&quot;: &#123;</span><br><span class="line">            &quot;attrIds&quot;: &#123;</span><br><span class="line">              &quot;terms&quot;: &#123;</span><br><span class="line">                &quot;field&quot;: &quot;attrValueList.productAttributeId&quot;,</span><br><span class="line">                &quot;size&quot;: 10</span><br><span class="line">              &#125;,</span><br><span class="line">              &quot;aggs&quot;: &#123;</span><br><span class="line">                &quot;attrValues&quot;: &#123;</span><br><span class="line">                  &quot;terms&quot;: &#123;</span><br><span class="line">                    &quot;field&quot;: &quot;attrValueList.value&quot;,</span><br><span class="line">                    &quot;size&quot;: 10</span><br><span class="line">                  &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;attrNames&quot;: &#123;</span><br><span class="line">                  &quot;terms&quot;: &#123;</span><br><span class="line">                    &quot;field&quot;: &quot;attrValueList.name&quot;,</span><br><span class="line">                    &quot;size&quot;: 10</span><br><span class="line">                  &#125;</span><br><span class="line">                &#125;</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>比如我们搜索<code>小米</code>这个关键字的时候，聚合出了下面的分类和品牌信息；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_10-22aa0550.png"></p>
<ul>
<li>聚合出了<code>屏幕尺寸</code>为<code>5.0</code>和<code>5.8</code>的筛选属性信息；</li>
</ul>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/2024-02-02_22:52:09_product_search_11-a45bddb3.png"></p>
<ul>
<li>在SpringBoot中实现，聚合操作比较复杂，已经超出了Elasticsearch Repositories的使用范围，需要直接使用ElasticsearchTemplate来实现；</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * 商品搜索管理Service实现类</span><br><span class="line"> * Created by macro on 2018/6/19.</span><br><span class="line"> */</span><br><span class="line">@Service</span><br><span class="line">public class EsProductServiceImpl implements EsProductService &#123;</span><br><span class="line">    @Override</span><br><span class="line">    public EsProductRelatedInfo searchRelatedInfo(String keyword) &#123;</span><br><span class="line">        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();</span><br><span class="line">        //搜索条件</span><br><span class="line">        if(StringUtils.isEmpty(keyword))&#123;</span><br><span class="line">            builder.withQuery(QueryBuilders.matchAllQuery());</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            builder.withQuery(QueryBuilders.multiMatchQuery(keyword,&quot;name&quot;,&quot;subTitle&quot;,&quot;keywords&quot;));</span><br><span class="line">        &#125;</span><br><span class="line">        //聚合搜索品牌名称</span><br><span class="line">        builder.addAggregation(AggregationBuilders.terms(&quot;brandNames&quot;).field(&quot;brandName&quot;));</span><br><span class="line">        //集合搜索分类名称</span><br><span class="line">        builder.addAggregation(AggregationBuilders.terms(&quot;productCategoryNames&quot;).field(&quot;productCategoryName&quot;));</span><br><span class="line">        //聚合搜索商品属性，去除type=1的属性</span><br><span class="line">        AbstractAggregationBuilder aggregationBuilder = AggregationBuilders.nested(&quot;allAttrValues&quot;,&quot;attrValueList&quot;)</span><br><span class="line">                .subAggregation(AggregationBuilders.filter(&quot;productAttrs&quot;,QueryBuilders.termQuery(&quot;attrValueList.type&quot;,1))</span><br><span class="line">                .subAggregation(AggregationBuilders.terms(&quot;attrIds&quot;)</span><br><span class="line">                        .field(&quot;attrValueList.productAttributeId&quot;)</span><br><span class="line">                        .subAggregation(AggregationBuilders.terms(&quot;attrValues&quot;)</span><br><span class="line">                                .field(&quot;attrValueList.value&quot;))</span><br><span class="line">                        .subAggregation(AggregationBuilders.terms(&quot;attrNames&quot;)</span><br><span class="line">                                .field(&quot;attrValueList.name&quot;))));</span><br><span class="line">        builder.addAggregation(aggregationBuilder);</span><br><span class="line">        NativeSearchQuery searchQuery = builder.build();</span><br><span class="line">        return elasticsearchTemplate.query(searchQuery, response -&gt; &#123;</span><br><span class="line">            LOGGER.info(&quot;DSL:&#123;&#125;&quot;,searchQuery.getQuery().toString());</span><br><span class="line">            return convertProductRelatedInfo(response);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 将返回结果转换为对象</span><br><span class="line">     */</span><br><span class="line">    private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) &#123;</span><br><span class="line">        EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo();</span><br><span class="line">        Map&lt;String, Aggregation&gt; aggregationMap = response.getAggregations().getAsMap();</span><br><span class="line">        //设置品牌</span><br><span class="line">        Aggregation brandNames = aggregationMap.get(&quot;brandNames&quot;);</span><br><span class="line">        List&lt;String&gt; brandNameList = new ArrayList&lt;&gt;();</span><br><span class="line">        for(int i = 0; i&lt;((Terms) brandNames).getBuckets().size(); i++)&#123;</span><br><span class="line">            brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString());</span><br><span class="line">        &#125;</span><br><span class="line">        productRelatedInfo.setBrandNames(brandNameList);</span><br><span class="line">        //设置分类</span><br><span class="line">        Aggregation productCategoryNames = aggregationMap.get(&quot;productCategoryNames&quot;);</span><br><span class="line">        List&lt;String&gt; productCategoryNameList = new ArrayList&lt;&gt;();</span><br><span class="line">        for(int i=0;i&lt;((Terms) productCategoryNames).getBuckets().size();i++)&#123;</span><br><span class="line">            productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString());</span><br><span class="line">        &#125;</span><br><span class="line">        productRelatedInfo.setProductCategoryNames(productCategoryNameList);</span><br><span class="line">        //设置参数</span><br><span class="line">        Aggregation productAttrs = aggregationMap.get(&quot;allAttrValues&quot;);</span><br><span class="line">        List&lt;LongTerms.Bucket&gt; attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty(&quot;productAttrs&quot;)).getProperty(&quot;attrIds&quot;)).getBuckets();</span><br><span class="line">        List&lt;EsProductRelatedInfo.ProductAttr&gt; attrList = new ArrayList&lt;&gt;();</span><br><span class="line">        for (Terms.Bucket attrId : attrIds) &#123;</span><br><span class="line">            EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr();</span><br><span class="line">            attr.setAttrId((Long) attrId.getKey());</span><br><span class="line">            List&lt;String&gt; attrValueList = new ArrayList&lt;&gt;();</span><br><span class="line">            List&lt;StringTerms.Bucket&gt; attrValues = ((StringTerms) attrId.getAggregations().get(&quot;attrValues&quot;)).getBuckets();</span><br><span class="line">            List&lt;StringTerms.Bucket&gt; attrNames = ((StringTerms) attrId.getAggregations().get(&quot;attrNames&quot;)).getBuckets();</span><br><span class="line">            for (Terms.Bucket attrValue : attrValues) &#123;</span><br><span class="line">                attrValueList.add(attrValue.getKeyAsString());</span><br><span class="line">            &#125;</span><br><span class="line">            attr.setAttrValues(attrValueList);</span><br><span class="line">            if(!CollectionUtils.isEmpty(attrNames))&#123;</span><br><span class="line">                String attrName = attrNames.get(0).getKeyAsString();</span><br><span class="line">                attr.setAttrName(attrName);</span><br><span class="line">            &#125;</span><br><span class="line">            attrList.add(attr);</span><br><span class="line">        &#125;</span><br><span class="line">        productRelatedInfo.setProductAttrs(attrList);</span><br><span class="line">        return productRelatedInfo;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="#参考资料"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99">#</a>参考资料</h2><blockquote>
<p>关于Spring Data Elasticsearch的具体使用可以参考官方文档。</p>
</blockquote>
<p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring-data/elasticsearch/docs/3.2.6.RELEASE/reference/html/#reference">https://docs.spring.io/spring-data/elasticsearch/docs/3.2.6.RELEASE/reference/html/#reference</a></p>
<h2 id="项目地址"><a href="#项目地址" class="headerlink" title="#项目地址"></a><a target="_blank" rel="noopener" href="https://www.macrozheng.com/mall/technology/product_search.html#%E9%A1%B9%E7%9B%AE%E5%9C%B0%E5%9D%80">#</a>项目地址</h2><p><a target="_blank" rel="noopener" href="https://github.com/macrozheng/mall">https://github.com/macrozheng/mall</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2024/01/d8d5c5f413d7.html">https://ko25891wan.gitlab.io/2024/01/d8d5c5f413d7.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/01/c8555c416d6c.html" title="Java 8都出那么久了，Stream API了解下？"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java 8都出那么久了，Stream API了解下？</div></div></a></div><div class="next-post pull-right"><a href="/2024/01/222614040568.html" title="Elasticsearch 升级 7.x 版本后，我感觉掉坑里了！"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Elasticsearch 升级 7.x 版本后，我感觉掉坑里了！</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%91%8D-%E7%9B%B8%E5%85%B3%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B"><span class="toc-number">1.</span> <span class="toc-text">👍 相关视频教程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8"><span class="toc-number">2.</span> <span class="toc-text">中文分词器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8IKAnalyzer"><span class="toc-number">2.1.</span> <span class="toc-text">使用IKAnalyzer</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8SpringBoot%E4%B8%AD%E4%BD%BF%E7%94%A8"><span class="toc-number">2.2.</span> <span class="toc-text">在SpringBoot中使用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E5%95%86%E5%93%81%E6%90%9C%E7%B4%A2"><span class="toc-number">3.</span> <span class="toc-text">简单商品搜索</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%BC%E5%90%88%E5%95%86%E5%93%81%E6%90%9C%E7%B4%A2"><span class="toc-number">4.</span> <span class="toc-text">综合商品搜索</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E5%95%86%E5%93%81%E6%8E%A8%E8%8D%90"><span class="toc-number">5.</span> <span class="toc-text">相关商品推荐</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%81%9A%E5%90%88%E6%90%9C%E7%B4%A2%E5%95%86%E5%93%81%E7%9B%B8%E5%85%B3%E4%BF%A1%E6%81%AF"><span class="toc-number">6.</span> <span class="toc-text">聚合搜索商品相关信息</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">7.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E5%9C%B0%E5%9D%80"><span class="toc-number">8.</span> <span class="toc-text">项目地址</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>